﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Description;

namespace Frame.WcfService
{
    public class Process
    {
        static ServiceHost _ServiceHost;

        public static void Start(string ip, string port)
        {
            Type typebus = typeof(WcfService);

            Uri httpBaseAddress = new Uri("http://" + ip + ":" + port + "/Service" + "/" + "Wcf/");
            Uri tcpBaseAddress = new Uri("net.tcp://" + ip + ":" + (int.Parse(port) + 1).ToString() + "/Service" + "/" + "Wcf/");

            _ServiceHost = new ServiceHost(typebus, httpBaseAddress, tcpBaseAddress);
            {
                WSHttpBinding httpBinding = new WSHttpBinding();
                httpBinding.MaxBufferPoolSize = 104857600;
                httpBinding.MaxReceivedMessageSize = 104857600;
                httpBinding.Security.Mode = SecurityMode.None;
                httpBinding.SendTimeout = new TimeSpan(0, 10, 0);
                httpBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);

                NetTcpBinding tcpBinding = new NetTcpBinding();
                tcpBinding.MaxBufferSize = 104857600;
                tcpBinding.MaxReceivedMessageSize = 104857600;
                tcpBinding.MaxBufferPoolSize = 104857600;
                tcpBinding.Security.Mode = SecurityMode.None;
                tcpBinding.SendTimeout = new TimeSpan(0, 10, 0);
                tcpBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);

                _ServiceHost.AddServiceEndpoint(typeof(WcfContract.IWcfService), httpBinding, String.Empty);
                _ServiceHost.AddServiceEndpoint(typeof(WcfContract.IWcfService), tcpBinding, String.Empty);

                ServiceMetadataBehavior behavior = _ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (behavior == null)
                {
                    behavior = new ServiceMetadataBehavior();
                    behavior.HttpGetEnabled = true;
                    _ServiceHost.Description.Behaviors.Add(behavior);
                }
                else
                {
                    behavior.HttpGetEnabled = true;
                }

                ServiceDebugBehavior debug = _ServiceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
                if (behavior == null)
                {
                    debug = new ServiceDebugBehavior();
                    debug.IncludeExceptionDetailInFaults = true;
                    _ServiceHost.Description.Behaviors.Add(debug);
                }
                else
                {
                    debug.IncludeExceptionDetailInFaults = true;
                }

                foreach (ServiceEndpoint ep in _ServiceHost.Description.Endpoints)
                {
                    foreach (OperationDescription op in ep.Contract.Operations)
                    {
                        DataContractSerializerOperationBehavior dataContractBehavior =
                           op.Behaviors.Find<DataContractSerializerOperationBehavior>()
                                as DataContractSerializerOperationBehavior;
                        if (dataContractBehavior != null)
                        {
                            dataContractBehavior.MaxItemsInObjectGraph = 100000;
                        }
                    }
                }

                _ServiceHost.Open();
            }
        }

        public static void Stop()
        {
            if (_ServiceHost != null)
            {
                if (_ServiceHost.State != CommunicationState.Closed)
                {
                    _ServiceHost.Close();
                }
            }
        }
    }
}
